package cn.com.zzl.util;

import org.apache.commons.lang.StringUtils;

import java.text.ParsePosition;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

/**
 * Created by 朱正磊 on 2016/11/12.
 */
public class DateUtil {
	public static final String DEF_DATETIME_FORMAT = "yyyy-MM-dd HH:mm:ss";
	public static final String DEF_DATE_FORMAT = "yyyy-MM-dd";
	public static final String DATE_TIME_FORMAT = "yyyyMMddHHmmss";
	private static final SimpleDateFormat defDateFormat = new SimpleDateFormat(DEF_DATE_FORMAT);

	public static String dateToString(Date date, String format) {
		if (date == null)
			return null;
		SimpleDateFormat dateFmt = new SimpleDateFormat(format);

		return dateFmt.format(date);
	}

	public static Calendar getCalendar() {
		return Calendar.getInstance();
	}

	public static Date getSystemCurrentDate() {
		return getCalendar().getTime();
	}

	public static String[] curweek() {
		String[] week = new String[2];
		Calendar cal =Calendar.getInstance();
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		week[0] = df.format(cal.getTime());

		cal.add(Calendar.WEEK_OF_YEAR, 1);
		week[1] = df.format(cal.getTime());
		return week;
	}

	public static String formatToDisplay(String input) {
		Date inputDate = strToDate(input, DATE_TIME_FORMAT);
		if (null != inputDate) {
			return dateToString(inputDate, DEF_DATE_FORMAT);
		}
		return null;
	}
	public static String formatToDisplay2(String input) {
		Date inputDate = strToDate(input, DATE_TIME_FORMAT);
		if (null != inputDate) {
			return dateToString(inputDate, DEF_DATETIME_FORMAT);
		}
		return null;
	}
	/**
	 * 字符串转换成日期
	 * @param dateStr : 日期字符串
	 * @param format  : 想要得到的格式
	 * @return Date
	 */
	public static Date strToDate(String dateStr, String format) {
		if (StringUtils.isBlank(dateStr))
			return null;
		if (StringUtils.isBlank(format))
			format = dateStr.contains(":") ? DEF_DATETIME_FORMAT : DEF_DATE_FORMAT;

		SimpleDateFormat dateFmt = new SimpleDateFormat(format);

		dateFmt.setLenient(false);		//指定是否对日期格式进行容错分析
		ParsePosition pos = new ParsePosition(0);
		return dateFmt.parse(dateStr, pos);
	}

	/**
	 * 字符串转换成日期（"yyyy-MM-dd"格式）
	 * @param str   : 日期字符串
	 * @return Date
	 */
	public static Date strToDate(String str) {
		return strToDate(str, DEF_DATE_FORMAT);
	}

	//获得当天0点时间
	public static Date getTimesmorning(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 0);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	//获得当天24点时间
	public static Date getTimesnight(){
		Calendar cal = Calendar.getInstance();
		cal.set(Calendar.HOUR_OF_DAY, 24);
		cal.set(Calendar.SECOND, 0);
		cal.set(Calendar.MINUTE, 0);
		cal.set(Calendar.MILLISECOND, 0);
		return cal.getTime();
	}
	//获得本周一0点时间
	public static Date getTimesWeekmorning(){
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		return cal.getTime();
	}
	//获得本周日24点时间
	public static Date getTimesWeeknight(){
		Calendar cal = Calendar.getInstance();
		cal.set(cal.get(Calendar.YEAR),cal.get(Calendar.MONDAY), cal.get(Calendar.DAY_OF_MONTH), 0, 0,0);
		cal.set(Calendar.DAY_OF_WEEK, Calendar.MONDAY);
		cal.add(5,7);
		return cal.getTime();
	}

	/**
	 * 得到系统日期
	 * @return Date
	 */
	public static Date getSystemDate() {
		Date date = new Date();
		String dateString = defDateFormat.format(date);
		Date nowDate = defDateFormat.parse(dateString, new ParsePosition(0));

		return nowDate;
	}
	/**
	 * 完整的日期时间转换成仅日期
	 * @param date   : 时间
	 * @return Date  : yyyy-MM-dd
	 */
	public static Date datetimeToDate(Date date) {
		if (date==null)
			return null;

		SimpleDateFormat dateFmt = new SimpleDateFormat(DEF_DATE_FORMAT);
		//指定是否对日期格式进行容错分析
		dateFmt.setLenient(false);
		ParsePosition pos = new ParsePosition(0);
		String str = dateToString(date,DEF_DATETIME_FORMAT);
		return dateFmt.parse(str, pos);
	}
}
